{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#!/usr/bin/python3\n",
    "# -*- coding: utf-8 -*-\n",
    "# Created by Ross on 19-3-22\n",
    "import os\n",
    "\n",
    "import numpy as np\n",
    "import tensorflow as tf\n",
    "from keras.preprocessing.sequence import pad_sequences\n",
    "\n",
    "import utils\n",
    "from hparams import Hparams\n",
    "from model.JointModel import JointModel\n",
    "\n",
    "tfe = tf.contrib.eager\n",
    "\n",
    "hparams = Hparams()\n",
    "hp = hparams.parser.parse_args()  # 超参数字典\n",
    "utils.save_hparams(hp, hp.log_dir)\n",
    "\n",
    "test_x = np.load(os.path.join(hp.data_dir, 'test_x.npy'))\n",
    "test_y = np.load(os.path.join(hp.data_dir, 'test_y.npy'))\n",
    "\n",
    "test_lens = [min(len(x), hp.seq_maxlen) for x in test_x]\n",
    "test_x = pad_sequences(test_x, hp.seq_maxlen, 'float32', padding='post', truncating='post')\n",
    "\n",
    "\n",
    "# test_y = np.where(test_y == 3, 1, 0).reshape(len(test_y), 1)\n",
    "# test_y = to_categorical(test_y, 2, 'int32')\n",
    "\n",
    "\n",
    "def train_domain():\n",
    "    for fold in range(10):\n",
    "        train_x, train_y, dev_x, dev_y = utils.get_single_fold(hp.data_dir, fold)\n",
    "\n",
    "        train_seq_lens = [min(len(x), hp.seq_maxlen) for x in train_x]\n",
    "        train_x = pad_sequences(train_x, hp.seq_maxlen, 'float32', padding='post', truncating='post')\n",
    "        dev_seq_lens = [min(len(x), hp.seq_maxlen) for x in dev_x]\n",
    "        dev_x = pad_sequences(dev_x, hp.seq_maxlen, 'float32', padding='post', truncating='post')\n",
    "        # 将数据和相应的长度打包，防止shuffle的时候打乱\n",
    "        # 打包后变成 [(x, seq_len)...]\n",
    "        train_x = list(zip(train_x, train_seq_lens))\n",
    "        dev_x = list(zip(dev_x, dev_seq_lens))\n",
    "\n",
    "        # train_y = np.where(train_y == 3, 1, 0)\n",
    "        # dev_y = np.where(dev_y == 3, 1, 0)\n",
    "        # train_y = to_categorical(train_y, 2, 'int32')\n",
    "        # dev_y = to_categorical(dev_y, 2, 'int32')\n",
    "        tf.reset_default_graph()\n",
    "\n",
    "        model = JointModel(hp.seq_maxlen, hp.emb_size, hp.rnn_size, hp.fake_task, hp.num_class, hp.use_crf,\n",
    "                           ntags=hp.ntags)\n",
    "        optimizer = tf.train.AdamOptimizer(hp.lr)\n",
    "\n",
    "        # 指标\n",
    "        # tf_label = tf.placeholder()\n",
    "        dev_acc = tfe.metrics.Accuracy()\n",
    "        test_acc = tfe.metrics.Accuracy()\n",
    "        for i in range(hp.epochs):\n",
    "            dev_acc.init_variables()\n",
    "            test_acc.init_variables()\n",
    "            for x, y in utils.generate_batch(train_x, train_y, 32, shuffle=True, undersampling=False):\n",
    "                with tf.GradientTape() as tape:\n",
    "                    # 使用zip(*x)解包，将[(x, seq_len)...] 拆分为 [x], [seq_len]两个\n",
    "                    pred = model(zip(*x))\n",
    "                    loss = model.domain_loss_func(pred, y)\n",
    "                    g = tape.gradient(loss, model.trainable_variables)\n",
    "                    optimizer.apply_gradients(zip(g, model.trainable_variables))\n",
    "\n",
    "            dev_pred = model.predict_domain(zip(*dev_x))\n",
    "            dev_acc(dev_y.flatten(), dev_pred)\n",
    "            print('dev_acc:', dev_acc.result().numpy())\n",
    "\n",
    "            test_pred = model.predict_domain([test_x, test_lens])\n",
    "            test_acc(test_y.flatten(), test_pred)\n",
    "            print('test_acc', test_acc.result().numpy())\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    train_domain()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
